#include "utils/format_ipv6.h"

size_t fmt_ipv6(char *target, char *src) {
  char key[] = "0123456789ABCDEF";

  size_t index = 0;
  size_t total = 0;
  // 使用点符号进行字符串分割
  char *temp = strtok(src, ".");
  while (temp) {
    uint8_t num = atoi(temp);

    // 将数字转为HEX文本
    target[index++] = key[num >> 4 & 0x0f];
    target[index++] = key[num & 0x0f];

    // 判断避并填充:符号
    if ((++total % 2) == 0 && index < 39) {
      target[index++] = ':';
    }

    // 继续进行字符串分割
    temp = strtok(NULL, ".");
  }

  return total;
}